package com.woniu.day8.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.day8.pojo.Parking;
import com.woniu.day8.pojo.UserInfo;
import com.woniu.day8.pojo.dto.ParkingDto;
import com.woniu.day8.service.ParkingService;
import com.woniu.day8.util.ResultMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/parking")
@CrossOrigin
public class ParkingController {
    @Autowired
   private ParkingService ParkingService;
    /**
     * 查询所有车位
     */
    @RequestMapping("/selectParking")
    public ResultMessage selectParking(@RequestBody ParkingDto parkingDto){
        Long pageNum = parkingDto.getPageNum(); // 拿到页码
        Long pageSize = parkingDto.getPageSize(); // 拿到页大小

        Parking parking = new Parking();
        BeanUtils.copyProperties(parkingDto,parking);
        Page<UserInfo> page = new Page<>(pageNum, pageSize);
        List<Parking> list = ParkingService.queryParking(page, parking);

        Map<String, Object> result = new HashMap<>();
        result.put("records", list); // 查询数据
        result.put("total", page.getTotal()); // 数据总条数
        result.put("current", page.getCurrent()); // 当前页
        result.put("pages", page.getPages()); // 总页数
        result.put("size", page.getSize()); // 页大小

        if (list != null && !list.isEmpty()){
            return ResultMessage.success(result);
        }
        return ResultMessage.fail("获取失败");
    }
    /**
     * 添加车位
     */
    @RequestMapping("/insertParking")
    public ResultMessage insertParking(@RequestBody ParkingDto parkingDto){
        Parking parking = new Parking();
        BeanUtils.copyProperties(parkingDto,parking);
        boolean b = ParkingService.save(parking);
        if (b){
            return ResultMessage.success("添加成功");
        }
        return ResultMessage.fail("添加失败");
    }
    /**
     * 修改车位
     */
    @RequestMapping("/updateParking")
    public ResultMessage updateParking(@RequestBody ParkingDto parkingDto){
        Parking parking = new Parking();
        BeanUtils.copyProperties(parkingDto,parking);
        boolean b = ParkingService.updateById(parking);
        if (b){
            return ResultMessage.success("修改成功");
        }
        return ResultMessage.fail("修改失败");
    }
    /**
     * 删除车位
     */
    @RequestMapping("/deleteParking")
    public ResultMessage deleteParking(@RequestBody ParkingDto parkingDto){
        boolean b = ParkingService.removeById(parkingDto.getId());
        if (b){
            return ResultMessage.success("删除成功");
        }
        return ResultMessage.fail("删除失败");
    }
}
